package com.imedway.ihe.client;

import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryResponse;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.ResponseOptionType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.AdhocQueryType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.SlotListType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.SlotType1;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ValueListType;
import org.openehealth.ipf.commons.ihe.xds.iti18.Iti18PortType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/3
 */
@Slf4j
@RestController
@RequestMapping("iti18")
public class Iti18PortTypeClient {

    @PostMapping
    public void iti18() throws MalformedURLException {
        URL wsdlUrl = new URL("http://localhost:8080/services/iti18?wsdl");
        QName serviceName = new QName("http://service.ihe.imedway.com/", "Iti18PortTypeImplService");
        Service service = Service.create(wsdlUrl, serviceName);
        Iti18PortType myWebService = service.getPort(Iti18PortType.class);
        AdhocQueryRequest adhocQueryRequest = new AdhocQueryRequest();

        adhocQueryRequest.setId("test");
        adhocQueryRequest.setComment("ddddddddddddd");
        adhocQueryRequest.setFederated(true);
        ResponseOptionType ro = new ResponseOptionType();
        ro.setReturnType("LeafClass");
        ro.setReturnComposedObjects(true);
        adhocQueryRequest.setResponseOption(ro);
        adhocQueryRequest.setMaxResults(new BigInteger("100"));
        adhocQueryRequest.setStartIndex(new BigInteger("1"));
        adhocQueryRequest.setFederation("https://www.baidu.com/");


        AdhocQueryType adhocQuery = new AdhocQueryType();
        adhocQuery.setId("urn:uuid:14d4debf-8f97-4251-9a74-a90016b0af0d");
        adhocQuery.setHome("urn:oid:1.2.3");

        SlotListType slotListType = new SlotListType();

        List<SlotType1> slotList = new ArrayList<SlotType1>();
        SlotType1 slotType1 = new SlotType1();
        slotType1.setName("$XDSDocumentEntryPatientId");
        ValueListType valueListType = new ValueListType();
        List<String> value = new ArrayList<String>();
        value.add("st3498702^^^&amp;1.3.6.1.4.1.21367.2005.3.7&amp;ISO");
        slotType1.setValueList(valueListType);
        slotList.add(slotType1);
        slotListType.getSlot().addAll(slotList);

        adhocQueryRequest.setRequestSlotList(slotListType);

        adhocQuery.getSlot().addAll(slotList);

        adhocQueryRequest.setAdhocQuery(adhocQuery);
        AdhocQueryResponse adhocQueryResponse = myWebService.documentRegistryRegistryStoredQuery(adhocQueryRequest);
        log.info("---adhocQueryResponse---" + adhocQueryResponse.getRequestId());
        log.info("---adhocQueryResponse---" + adhocQueryResponse.getRegistryErrorList().getRegistryError().get(0).getErrorCode());
    }

}